package cn.edu.scau.cmi.chenmiaopeng.controller.Hibernate;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import cn.edu.scau.cmi.chenmiaopeng.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.domain.Student;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.domain.Teacher;
public class HibernateFrameController {
        @FXML
	    private Button confirm;

	    @FXML
	    private ComboBox<String> combobox1;

	    @FXML
	    private ComboBox<?> combobox2;

	    @FXML
	    private RadioButton radio1;

	    @FXML
	    private TextArea textarea;

	    @FXML
	    private RadioButton radio2;

	    @FXML
	    private TextField textField1;
	    
	    @FXML
	    private TextField textField2;
       
	    private static ToggleGroup toggle;
	    private String originalId;     //原id，不能更改，逻辑在clickButton() changed中
	    private ChangeListener comboBoxListener1;  //功能下拉列表监听
	    private ChangeListener comboBoxListener2;  //下拉列表改变监听器
	    private boolean updateAuto = true;  //表示是否是刷新下拉列表
	    private String instruct_stu_id;
	    private String tutor_id;
//	    private boolean isAddStudent=true;  //变量表示老师是否添加学生
	    private boolean isAddTutor=false;  //该变量表示是否增加导师 true:增加 false:修改
	    //功能下拉框列表
	    private ObservableList<String> functions_t = FXCollections.observableArrayList("添加老师","仅修改老师名字","修改老师工号和名字","删除老师","添加指导学生","修改指导学生","删除指导学生","显示所有老师");
	    private ObservableList<String> functions_t_remove = FXCollections.observableArrayList("添加老师","仅修改老师名字","修改老师工号和名字","删除老师","添加指导学生","显示所有老师");
	    private ObservableList<String> functions_s = FXCollections.observableArrayList("添加学生","仅修改学生名字","修改学生学号和名字","删除学生","添加导师","显示所有学生");  
	    //若该学生有导师，则显示修改导师
	    private TeacherDAO teacherDAO = new TeacherDAO();
	    private StudentDAO studentDAO = new StudentDAO();
	    private int changeLevel = 1;  //表示修改指导学生的阶段
	    //枚举类型{添加、修改、删除指导学生}
	    public enum teacherOp{
	    	add,change,delete
	    }
	    //枚举变量
	    teacherOp teaOp;
	    ObservableList newStus=null;
	    private String old_Id_String;
	    public void setToggle(ToggleGroup tg) {
	    	toggle = tg;
//	    	combobox1 = box1;
//	    	combobox2 = box2;
	    }
	    private void printArea(String result) {
	    	textarea.clear();
	    	textarea.setText(result);
	    }
	    //更新功能框：
	    private void updateFunctionComboBox(int signal) {  
	    	//signal:0(正常更新) 1(需要更新老师功能（去除修改、删除）) 2(需要更新老师功能（全）)
//	    	System.out.println("更换实体。");
	    	//初始化combobox1:
	    	combobox1.getSelectionModel().selectedItemProperty().removeListener(comboBoxListener1);
	    	if(signal==0) {  
	    		combobox1.getSelectionModel().select(-1);
	    	}
	    	else if(signal==1) {  //为没有指导学生的老师过滤掉修改和删除
	    		combobox1.setItems(functions_t_remove);
	    	}
	    	else if(signal==2) {
	    		combobox1.setItems(functions_t);
	    	}
//	    	combobox2.setPromptText("请选择功能:");
	    	combobox1.getSelectionModel().selectedItemProperty().addListener(comboBoxListener1);
	    }
	    //清空输入框
	    private void clearTextField() {
	    	textField1.setText("");
	    	textField2.setText("");
	    }
	    //更改下拉框一信息
	    private void updateFunctionComboBoxToggle(String newToggleValue) {
	    	combobox1.getSelectionModel().selectedItemProperty().removeListener(comboBoxListener1);
	    	if(newToggleValue.equals("teacher")) {  //选择老师按钮
	    		combobox1.setItems(functions_t);
				combobox1.setPromptText("请选择功能：");
	    	}
	    	else if(newToggleValue.equals("student")) {
	    		functions_s.set(4, "添加导师");
	    		combobox1.setItems(functions_s);
				combobox1.setPromptText("请选择功能：");
	    	}
	    	combobox1.getSelectionModel().selectedItemProperty().addListener(comboBoxListener1);
	    }
//	    private void 
	    //更新下拉框二信息
	    private void updateItemComboBox() {
	    	//初始化combobox2:
	    	//先去除监听器：
	    	combobox2.getSelectionModel().selectedItemProperty().removeListener(comboBoxListener2);
	    	if(updateAuto) {  //表示只是每次增删改同一类型之后的更新
	    		if(radio1.isSelected()) {
		    		ObservableList newTeas = FXCollections.observableArrayList(teacherDAO.findAll());
			    	combobox2.setItems(newTeas);
			    	combobox2.setPromptText("请选择老师:");
		    	}
		    	else {
		    		ObservableList newStus = FXCollections.observableArrayList(studentDAO.findAll());
			    	combobox2.setItems(newStus);
			    	combobox2.setPromptText("请选择学生:");
		    	}
	    	}
	    	else {  //表示供老师选择学生、或者学生选择老师的选择功能，而不是更新
	    		if(radio1.isSelected()) {  //老师选学生，选的应是没有的学生，已选的学生不重复
	    			long id = Long.parseLong(textField1.getText());
    				Teacher tutor = teacherDAO.findById(id);
	    			if(teaOp==teacherOp.add) {
	    				List<Student> allStu = studentDAO.findAll();  
	    				Set<Student> allStus = new HashSet<Student>(allStu);
	    				Set<Student> tutorStus = tutor.getStudents();
	    				
	    				Set<Student> diffSet = new HashSet<Student>();
	    				diffSet.addAll(allStus);
	    				diffSet.removeAll(tutorStus);
//	    				System.out.println("教师没有的学生为："+diffSet);
	    				List<Student> stuList = new ArrayList<Student>(diffSet);
	    				ObservableList newStus = FXCollections.observableArrayList(diffSet);
				    	combobox2.setItems(newStus);
				    	combobox2.setPromptText("请为老师选择添加指导学生:");
	    			}
	    			else if(teaOp==teacherOp.change) {
	    				if(changeLevel==1) {
	    					ObservableList replaceStus = FXCollections.observableArrayList(tutor.getStudents());
					    	combobox2.setItems(replaceStus);
					    	if(replaceStus.size()==1) {
					    		combobox2.getSelectionModel().select(0);  //只有一个指导学生时就选该学生
					    		
					    		String selectItem = (combobox2.getSelectionModel().getSelectedItem()).toString();  //(Object) 
					    		int beginIndex = selectItem.indexOf(":");
			    				int endIndex = selectItem.indexOf(" ");
					    		instruct_stu_id = selectItem.substring(beginIndex+1,endIndex);
					    	}
					    	else combobox2.setPromptText("请为老师选择被替换的指导学生:");
					    	List<Student> allStu = studentDAO.findAll();  
		    				Set<Student> allStus = new HashSet<Student>(allStu);
		    				Set<Student> tutorStus = tutor.getStudents();
		    				Set<Student> diffSet = new HashSet<Student>();
		    				diffSet.addAll(allStus);
		    				diffSet.removeAll(tutorStus);
		    				List<Student> stuList = new ArrayList<Student>(diffSet);
		    				newStus = FXCollections.observableArrayList(diffSet);
	    				}
	    				else if(changeLevel==2) {
	    					if(newStus!=null) {
	    						combobox2.setItems(newStus);
	    						combobox2.setPromptText("请为老师选择要替换的指导学生:");
	    						newStus = null;
	    					}
	    					else System.out.println("不是该老师指导的学生列表为Null!");
	    				}
	    			}
	    			else if(teaOp==teacherOp.delete){
	    				ObservableList deleteStus = FXCollections.observableArrayList(tutor.getStudents());
				    	combobox2.setItems(deleteStus);
				    	combobox2.setPromptText("请为老师选择删除指导学生:");
	    			}
	    		}
	    		else {  //学生选老师
	    			List<Teacher> allTeas = teacherDAO.findAll();
	    			//如果是修改导师，过滤掉重复的导师
	    			if(isAddTutor) {
	    				ObservableList newTeas = FXCollections.observableArrayList(allTeas);
	    				combobox2.setItems(newTeas);
	    			}
	    			else {
	    				long id = Long.parseLong(textField1.getText());
	    				Student thisStudent = studentDAO.findById(id);
	    				Teacher duplicateTea = thisStudent.getTeacher();
//	    				duplicateTea = new Teacher(id,name);
//	    				for(Teacher t:allTeas) {
//	    					if(t.getId()==id) {
//	    						allTeas.remove(t);
//	    						break;
//	    					}
//	    				}
	    				allTeas.remove(duplicateTea);
	    				ObservableList newTeas = FXCollections.observableArrayList(allTeas);
	    				combobox2.setItems(newTeas);
	    			}
			    	combobox2.setPromptText("请为学生选择导师:");
	    		}
	    	}
	    	//改变后再加装监听器
	    	combobox2.getSelectionModel().selectedItemProperty().addListener(comboBoxListener2);
	    }
	    //点击确认按钮
	    @FXML 
	    public void clickButton() {
	    	if(radio1.isSelected()) {
	    		String function = combobox1.getSelectionModel().getSelectedItem();
	    		String id;
	    		String name;
	    		id=textField1.getText();
    			name = textField2.getText();
    			//删改的逻辑均是点击下拉框，则将原老师信息显示到输入框上，再让用户更改
    			if(function==null) {
	    			printArea("请选择功能下拉框的功能再点确认！");
	    			return ;
	    		}
	    		if(function.equals("添加老师")) {
	    			if(id.isEmpty()||name.isEmpty()) {
	    				printArea("请输入要添加的老师工号或名字！");
	    			}
	    			else {
	    				Long tea_id = Long.parseLong(id);
	    				Teacher newTea = new Teacher(tea_id,name);
	    				teacherDAO.save(newTea);
	    				printArea(teacherDAO.getResult());
	    				updateAuto = true;
	    				updateItemComboBox();
	    			}
	    		}
	    		else if(function.equals("仅修改老师名字")) {
	    			if(id.isEmpty()||name.isEmpty()) {
	    				printArea("请输入老师的工号和要修改的名字！");
	    			}
	    			else {
	    			  if(!id.equals(originalId)) {
	                        printArea("不能修改老师id,只能修改老师名字！");
	                        return ;
	    				}
	    				Long tea_id = Long.parseLong(id);
//	    				Teacher newTea = new Teacher(tea_id,name);
	    				teacherDAO.updateTeacherByName(tea_id, name);
	    				printArea(teacherDAO.getResult());
	    				updateAuto = true;
	    				updateItemComboBox();
	    			}
	    		}
	    		else if(function.equals("修改老师工号和名字")) {
	    			if(id.isEmpty()||name.isEmpty()) {
	    				printArea("请输入老师的工号和要修改的名字！");
	    			}
	    			else {
	    				Long oldStuId = Long.parseLong(originalId);
	    				Long newStuId = Long.parseLong(id);
	    				teacherDAO.updateTeacherIdAndName(oldStuId, newStuId, name);
	    				printArea(teacherDAO.getResult());
	    				updateAuto = true;
	    				updateItemComboBox();
	    			}
	    		}
	    		else if(function.equals("删除老师")) {
	    			if(id.isEmpty()) {
	    				printArea("请输入要删除的老师工号！");
	    			}
	    			else {
	    				long tea_id = Long.parseLong(id);
	    				Teacher del_tea = new Teacher(tea_id);
	    				teacherDAO.delete(del_tea);
	    				printArea(teacherDAO.getResult());
	    				updateAuto = true;
	    				updateItemComboBox();
	    			}
	    		}
	    		else if(function.equals("添加指导学生")) {
	    			if(instruct_stu_id.isEmpty()) {
	    				printArea("请选择指导学生，再点按钮！");
	    				return ;
	    			}
	    			long tea_id = Long.parseLong(id);
	    			long stu_id = Long.parseLong(instruct_stu_id);
	    			Student addStu = studentDAO.findById(stu_id);
	    			teacherDAO.addStudent(tea_id, addStu);
	    			printArea(teacherDAO.getResult());
	    			//恢复老师的操作：
	    			updateAuto = true;
	    			updateItemComboBox();
	    			updateFunctionComboBox(0);  //将功能框改为不选任何选项
	    			clearTextField();
	    			textField1.setEditable(true);
	    			textField2.setEditable(true);
	    			instruct_stu_id = "";
	    		}
	    		else if(function.equals("修改指导学生")) {
	    			if(instruct_stu_id.isEmpty()) {
	    				printArea("请选择指导学生，再点按钮！");
	    				return ;
	    			}
	    			if(changeLevel==1) {
	    				old_Id_String = instruct_stu_id;
	    				changeLevel = 2;
	    				//在这里更新下拉框2
	    				updateAuto = false;
	    				updateItemComboBox();
	    			}
	    			else if(changeLevel==2) {
	    				long tea_id = Long.parseLong(id);
	    				long old_stu_id = Long.parseLong(old_Id_String);
	    				Teacher teacher = teacherDAO.findById(tea_id);
	    				Student changedStu = studentDAO.findById(old_stu_id);
		    			long new_stu_id = Long.parseLong(instruct_stu_id);
		    			Student changeStu = studentDAO.findById(new_stu_id);
		    			teacherDAO.alterStudent(tea_id,changedStu,changeStu);
		    			String result = teacherDAO.getResult();
		    			
		    			if(result=="alter instruct student successful!") {
		    				String notes = "\nalter teacher ("+teacher+")'s instruct student \nfrom ("+changedStu+") \nto ("+changeStu+")";
		    				printArea(result+notes);
		    			}
		    			else printArea(result);
		    			//恢复老师的操作：
		    			updateAuto = true;
		    			updateItemComboBox();
		    			updateFunctionComboBox(0);  //将功能框改为不选任何选项
		    			clearTextField();
		    			textField1.setEditable(true);
		    			textField2.setEditable(true);
		    			instruct_stu_id = "";
		    			changeLevel = 0;
	    			}
	    		}
	    		else if(function.equals("删除指导学生")) {
	    			if(instruct_stu_id.isEmpty()) {
	    				printArea("请选择指导学生，再点按钮！");
	    				return ;
	    			}
	    			long tea_id = Long.parseLong(id);
	    			long stu_id = Long.parseLong(instruct_stu_id);
	    			Student deleteStu = studentDAO.findById(stu_id);
	    			teacherDAO.deleteStudent(tea_id, deleteStu);
	    			printArea(teacherDAO.getResult());
	    			//恢复老师的操作：
	    			updateAuto = true;
	    			updateItemComboBox();
	    			updateFunctionComboBox(0);  //将功能框改为不选任何选项
	    			clearTextField();
	    			textField1.setEditable(true);
	    			textField2.setEditable(true);
	    			instruct_stu_id = "";
	    		}
	    		else if(function.equals("显示所有老师")) {
	    			StringBuffer sb = new StringBuffer();
	    			List teas = teacherDAO.findAll();
	    			Iterator<?> teaIterator = teas.iterator();
	    			while(teaIterator.hasNext()){
	    				Teacher teacher = (Teacher) teaIterator.next();
	    				sb.append("老师名是："+teacher.getName()+"工号是:"+teacher.getId()+"\n");  //+
	    				Set<Student> instr_stus = teacher.getStudents();
	    				if(instr_stus.size()==0) {
	    					 sb.append("-该老师没有指导学生。\n");
	    				}
	    				else {
	    					 sb.append("-该老师指导了"+instr_stus.size()+"学生。\n");
	    					 Iterator<Student> stu_itor = instr_stus.iterator();
	    					 while(stu_itor.hasNext()) {
	    						 Student stu = stu_itor.next();
	    						 sb.append("--该老师指导的学生学号是："+stu.getId()+" 姓名是："+stu.getName()+"\n");
	    					 }
//	    					 System.out.println();
	    				}
	    				 sb.append("\n");
	    			}
	    			printArea(new String(sb));
	    		}
	    	}
	    	else if(radio2.isSelected()) {  //选择学生
	    		String function = combobox1.getSelectionModel().getSelectedItem();
	    		String id;
	    		String name;
	    		id=textField1.getText();
    			name = textField2.getText();
    			//"添加学生","修改学生","删除学生","添加导师","显示所有学生"
	    		if(function==null) {
	    			printArea("请选择功能下拉框的功能再点确认！");
	    			return ;
	    		}
	    		else if(function=="添加学生") {
	    			if(id.isEmpty()||name.isEmpty()) {
	    				printArea("请输入要添加的学生学号或名字！");
	    			}
	    			else {
	    				Long stu_id = Long.parseLong(id);
	    				Student newStu = new Student(stu_id,name);
	    				studentDAO.save(newStu);
	    				printArea(studentDAO.getResult());
	    				updateAuto = true;
	    				updateItemComboBox();
	    			}
	    		}
	    		else if(function=="仅修改学生名字") {
	    			if(id.isEmpty()||name.isEmpty()) {
	    				printArea("请输入学生的学号和要修改的名字！");
	    			}
	    			else {
	    				//仅修改学生name,不修改id
	    				 if(!id.equals(originalId)) {
		                        printArea("不能修改学生id,只能修改学生名字！");
		                        return ;
		    				}
		    				Long stu_id = Long.parseLong(id);
		    				studentDAO.updateStudentByName(stu_id, name);
		    				printArea(studentDAO.getResult());
		    				updateAuto = true;
		    				updateItemComboBox();
	    			}
	    		}
	    		else if(function=="修改学生学号和名字") {
	    			if(id.isEmpty()||name.isEmpty()) {
	    				printArea("请输入学生的学号和要修改的名字！");
	    			}
	    			else {
	    				//即修改name,又修改id
	    				Long oldStuId = Long.parseLong(originalId);
	    				Long newStuId = Long.parseLong(id);
	    				
	    				studentDAO.updateStudentIdAndName(oldStuId, newStuId, name);
	    				printArea(studentDAO.getResult());
	    				
		    				updateAuto = true;
		    				updateItemComboBox();
	    			}
	    		}
	    		else if(function=="删除学生") {
	    			if(id.isEmpty()) {
	    				printArea("请输入要删除的学生学号！");
	    			}
	    			else {
	    				long stu_id = Long.parseLong(id);
	    				Student del_stu = new Student(stu_id);
	    				studentDAO.delete(del_stu);
	    				printArea(studentDAO.getResult());
	    				updateAuto = true;
	    				updateItemComboBox();
	    				printArea(studentDAO.getResult());
	    			}
	    		}
	    		else if(function=="添加导师") {
	    			if(tutor_id.isEmpty()) {
	    				printArea("请选择一位导师，再点按钮！");
	    				return ;
	    			}
	    			long stu_id = Long.parseLong(id);
	    			long tea_id = Long.parseLong(tutor_id);
//	    			Student addStu = studentDAO.findById(stu_id);
	    			studentDAO.addTutor(tea_id, stu_id);
	    			printArea(studentDAO.getResult());
	    			
	    			//恢复老师的操作：
	    			updateAuto = true;
	    			updateItemComboBox();
	    			updateFunctionComboBox(0);  //将功能框改为不选任何选项
	    			clearTextField();
	    			textField1.setEditable(true);
	    			textField2.setEditable(true);
	    			tutor_id = "";
	    			printArea(teacherDAO.getResult());
	    		}
	    		else if(function=="修改导师") {
	    			if(tutor_id.isEmpty()) {
	    				printArea("请选择一位导师，再点按钮！");
	    				return ;
	    			}
	    			long stu_id = Long.parseLong(id);
	    			long tea_id = Long.parseLong(tutor_id);
//	    			Student addStu = studentDAO.findById(stu_id);
	    			studentDAO.alterTutor(tea_id, stu_id);
	    			printArea(studentDAO.getResult());
	    			
	    			//恢复老师的操作：
	    			updateAuto = true;
	    			updateItemComboBox();
	    			updateFunctionComboBox(0);  //将功能框改为不选任何选项
	    			clearTextField();
	    			textField1.setEditable(true);
	    			textField2.setEditable(true);
	    			tutor_id = "";
	    			printArea(teacherDAO.getResult());
	    		}
	    		else if(function=="显示所有学生") {
	    			StringBuffer sb = new StringBuffer();
	    			List<?> stus = studentDAO.findAll();
	    			Iterator<?> stuIterator = stus.iterator();
	    			while(stuIterator.hasNext()){
	    				Student stu = (Student) stuIterator.next();
	    				sb.append("学生名是："+stu.getName()+" 学号是:"+stu.getId()); 
	    				if(stu.getTeacher()==null) sb.append(", 没有指导老师！\n");
	    				else sb.append(" 指导老师工号: "+stu.getTeacher().getId()+",姓名: "+stu.getTeacher().getName()+"\n");
	    				sb.append("\n");  //输出的每个学生之间隔一空行
	    		    }
	                printArea(new String(sb));
	    		}
	    		//其余情况跳过不处理
	    	}
	    }
	    //给comboBox增加监听器
	    public void addComboBoxListener() {
	    	comboBoxListener2 = new ChangeListener() {
	    		public void changed(ObservableValue changed, Object oldVal, Object newVal) {
	    			System.out.println(newVal);
	    			if(updateAuto) {
	    				String regex = ":";
		    			String[] splitStr = newVal.toString().split(regex);
		    			int index = splitStr[1].indexOf(" ");
		    			String id = splitStr[1].substring(0,index);
		    			originalId = id;
		    			String name = splitStr[2];
		    			textField1.setText(id);
		    			textField2.setText(name);
		    			if(radio1.isSelected()) {
		    				long teacherId = Long.parseLong(id);
		    				Teacher tea = teacherDAO.findById(teacherId);
		    				if(tea.getStudents().size()==0) {  //去除修改学生和删除学生功能
		    					updateFunctionComboBox(1);
		    				}
		    				else {  //该老师至少有一名学生的情况,全部功能都要加上
//		    					combobox1.setItems(functions_t);
//		    					combobox1.setPromptText("请选择功能：");
		    					updateFunctionComboBox(2);
		    				}
		    			}
		    			else if(radio2.isSelected()) {
		    				long stuId = Long.parseLong(originalId);
		    				   Student stu = studentDAO.findById(stuId);
		    				   if(stu.getTeacher()==null) {
		    					   combobox1.getItems().set(4, "添加导师");
		    				   }
		    				   else {
		    					   combobox1.getItems().set(4, "修改导师");
		    					   //会改变功能？ 会
		    				   }
		    					updateFunctionComboBox(0);  //改变了个体以后就要更新功能框，使其指向默认的提示
		    			}
		    		
	    			}
	    			else {
	    				int beginIndex = newVal.toString().indexOf(":");
	    				int endIndex = newVal.toString().indexOf(" ");
	    				if(radio1.isSelected()) {
	    					instruct_stu_id = newVal.toString().substring(beginIndex+1, endIndex);
	    				}
	    				else tutor_id = newVal.toString().substring(beginIndex+1, endIndex);
	    				printArea("当前选择的是："+newVal);
	    			}
	    		}
	    	};
	    	combobox2.getSelectionModel().selectedItemProperty().addListener(comboBoxListener2);
	    	//功能监听器：
	    	comboBoxListener1 = new ChangeListener<Object>() {
	    	 public void changed(ObservableValue changed, Object oldVal, Object newVal) {
	    		 
	    		 if(radio1.isSelected()) {
	    			 if(newVal.toString().equals("添加指导学生")) {
	    				 Object selected = combobox2.getSelectionModel().getSelectedItem();
	    				 if(selected==null) {
	    					 printArea("请选择一位导师再添加学生！");
	    					 return ;
	    				 }
		    				printArea("请在下拉框二选择要添加的学生。");
			    			textField1.setEditable(false);
			    			textField2.setEditable(false);
			    			updateAuto = false;
//			    			isAddStudent = true;
			    			teaOp = teacherOp.add;
			    			updateItemComboBox();
		    		 }
	    			 else if(newVal.toString().equals("删除指导学生")) {
	    				 Object selected = combobox2.getSelectionModel().getSelectedItem();
	    				 if(selected==null) {
	    					 printArea("请选择一位导师再删除学生！");
	    					 return ;
	    				 }
	    				 printArea("请在下拉框二选择要删除的学生。");
			    			textField1.setEditable(false);
			    			textField2.setEditable(false);
			    			updateAuto = false;
//			    			isAddStudent = false;
			    			teaOp = teacherOp.delete;
			    			updateItemComboBox();
	    			 }
	    			 else if(newVal.toString().equals("修改指导学生")) {
	    				 Object selected = combobox2.getSelectionModel().getSelectedItem();
	    				 if(selected==null) {
	    					 printArea("请选择一位导师再修改指导学生！");
	    					 return ;
	    				 }
	    				 printArea("请先在下拉框二选择被替换的指导学生。");
	    				    textField1.setEditable(false);
			    			textField2.setEditable(false);
			    			updateAuto = false;
			    			teaOp = teacherOp.change;
			    			updateItemComboBox();
			    			changeLevel = 1;
	    			 }
	    		 }
	    		 else if(radio2.isSelected()) {
	    			if(newVal.toString().equals("添加导师")) {
	    				boolean notSelectedTutor = combobox2.getSelectionModel().isEmpty();
	    				   if(notSelectedTutor) {
	    					   printArea("请选择一位学生再添加导师！");
	    					   return ;
	    				   }
	    				   
			    			textField1.setEditable(false);
			    			textField2.setEditable(false);
			    			updateAuto = false;
			    			isAddTutor = true;
			    			printArea("请在下拉框二选择一位导师");
			    			updateItemComboBox();
		    		 }
	    			else if(newVal.toString().equals("修改导师")) {
	    				boolean notSelectedTutor = combobox2.getSelectionModel().isEmpty();
	    				   if(notSelectedTutor) {
	    					   printArea("请选择一位学生再修改导师！");
	    					   return ;
	    				   }
	    				    textField1.setEditable(false);
			    			textField2.setEditable(false);
			    			updateAuto = false;
			    			isAddTutor = false;
			    			printArea("请在下拉框二选择一位导师");
			    			updateItemComboBox();
	    			}
	    		 }
	    	}
	    };
	    	combobox1.getSelectionModel().selectedItemProperty().addListener(comboBoxListener1);
	    }
	    //给toggleGroup添加监听器
	    public void addToggleListener() {
	    	ChangeListener<Toggle> toggleChangeListener = new ChangeListener<Toggle>(){			
		    	@Override			
		    	public void changed(ObservableValue<? extends Toggle> changed, Toggle oldVal, Toggle newVal)			
		    	{				
		    			
//		    				List teacherList = teacherDAO.findAll();  //初始化老师下拉框
//		    				ComboBox combobox2 = (ComboBox)loader.getNamespace().get("comobox2");
//		    				ObservableList teachers = FXCollections.observableArrayList(teacherList);
//		    				combobox2.setItems(teachers);
//		    				combobox2.setPromptText("请选择老师:");
		    				
		    				updateAuto = true;
		    				updateFunctionComboBoxToggle(newVal.getUserData().toString());
		    				updateItemComboBox();
		    				textField1.clear();
		    				textField2.clear();
		    			  //选择学生按钮
//		    				List studentList = studentDAO.findAll();  //初始化老师下拉框
//		    				ComboBox combobox2 = (ComboBox)loader.getNamespace().get("comobox2");
//		    				ObservableList students = FXCollections.observableArrayList(studentList);
//		    				System.out.println(combobox2);
//		    				combobox2.setItems(students);
//		    				combobox2.setPromptText("请选择学生:");
		                    
		    	}	
		    };
		    toggle.selectedToggleProperty().addListener(toggleChangeListener);
	    }
	    //给toggle添加监听器
	        
	    
}
